package org.jeecg.modules.friends.moments.controller;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.ControllerMethodCheck;
import org.jeecg.common.aspect.annotation.AutoLog;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.friends.moments.dto.MomentsInfoDetailDTO;
import org.jeecg.modules.friends.moments.entity.Moments;
import org.jeecg.modules.friends.moments.param.DeleteMomentsParam;
import org.jeecg.modules.friends.moments.param.LikeMomentsParam;
import org.jeecg.modules.friends.moments.param.MomentsPageRequest;
import org.jeecg.modules.friends.moments.param.PublishMomentsParam;
import org.jeecg.modules.friends.moments.service.IMomentsService;

import org.jeecg.modules.friends.momentspics.service.IMomentsPicsService;
import org.jeecg.modules.friends.user.entity.User;
import org.jeecg.modules.friends.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;

/**
 * @Description: 动态信息表
 * @Author: jeecg-boot
 * @Date: 2024-05-15
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "动态信息")
@RestController
@RequestMapping("/friends/moments/")
public class MomentsController extends JeecgController<Moments, IMomentsService> {
    @Autowired
    private IMomentsService momentsService;
    @Autowired
    private IUserService userService;

    @Autowired
    private IMomentsPicsService momentsPicsService;

    @ControllerMethodCheck
    @ApiOperation(value = "动态信息-分页列表查询", notes = "动态信息-分页列表查询")
    @PostMapping(value = "/list")
    public Result<IPage<MomentsInfoDetailDTO>> queryPageList(@RequestBody MomentsPageRequest momentsPageRequest,
            @RequestHeader(value = "token") String token) {
        User user = userService.getUserByPhone(JwtUtil.getMobile(token));
        if (user == null) {
            return Result.error("登录过期，请重新登录");
        }
        IPage<MomentsInfoDetailDTO> pageList = momentsService.queryMomentsPage(momentsPageRequest, user);
        return Result.OK(pageList);
    }

    @ControllerMethodCheck
    @ApiOperation(value = "动态信息-分页列表查询主页", notes = "动态信息-分页列表查询")
    @PostMapping(value = "/list/home")
    public Result<IPage<MomentsInfoDetailDTO>> queryPageListHome(@RequestBody MomentsPageRequest momentsPageRequest,
            @RequestHeader(value = "token") String token) {
        User user = userService.getUserByPhone(JwtUtil.getMobile(token));
        if (user == null) {
            return Result.error("登录过期，请重新登录");
        }
        IPage<MomentsInfoDetailDTO> pageList = momentsService.queryMomentsPage(momentsPageRequest, user);
        return Result.OK(pageList);
    }

    /**
     * 添加
     */
    @ApiOperation(value = "点赞", notes = "点赞")
    @PostMapping(value = "/likeMoments")
    public Result<?> likeMoments(@RequestBody LikeMomentsParam param, @RequestHeader(value = "token") String token) {
        User user = userService.getUserByPhone(JwtUtil.getMobile(token));
        if (user == null) {
            return Result.error("登录过期，请重新登录");
        }
        momentsService.likeMoments(param, user.getId());
        return Result.OK("点赞成功！");
    }


    /**
     * 添加
     */
    @ControllerMethodCheck
    @ApiOperation(value = "动态信息表-添加", notes = "动态信息表-添加")
    @PostMapping(value = "/publishMoments")
    public Result<?> publishMoments(@RequestBody PublishMomentsParam param,
            @RequestHeader(value = "token") String token) {
        User user = userService.getUserByPhone(JwtUtil.getMobile(token));
        if (user == null) {
            return Result.error("登录过期，请重新登录");
        }
        momentsService.publishMoments(param, user.getId());
        return Result.OK("添加成功！");
    }


    /**
     * 通过id删除
     */
    @ControllerMethodCheck
    @ApiOperation(value = "动态信息-通过id删除", notes = "动态信息表-通过id删除")
    @PostMapping(value = "/deleteMoments")
    public Result<?> delete(@RequestBody DeleteMomentsParam param, @RequestHeader(value = "token") String token) {
        User user = userService.getUserByPhone(JwtUtil.getMobile(token));
        if (user == null) {
            return Result.error("登录过期，请重新登录");
        }
        Moments moments = momentsService.getById(param.getMomentsId());
        if (moments == null) {
            return Result.error("动态不存在");
        }
        if (!moments.getUserId().equals(user.getId())) {
            return Result.error("没有操作权限");
        }
        momentsService.deleteById(param.getMomentsId());
        return Result.OK("删除成功!");
    }
}
